<?php
namespace app\common\controller;

use think\Controller;
use Wechat\Wxobjopen;
class Excel extends Controller
{

    public function upload($file, $ext, $mid)
    {
        set_time_limit(0);
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Writer.Excel5");
        import("Org.Util.PHPExcel.IOFactory.php");
        import("Org.Util.PHPExcel.Reader.Excel5");

        $objPHPExcel = new \PHPExcel();

        $file_name = $file;

        if ($ext == "xlsx") {
            $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
        } else {
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
        }

        $objPHPExcel = $objReader->load($file_name, $encode = 'utf-8');
        $sheet = $objPHPExcel->getSheet(0);
        $highestRow = $sheet->getHighestRow(); // 取得总行数
        $highestColumn = $sheet->getHighestColumn(); // 取得总列数
//        $arrExcel = $objPHPExcel->getSheet(0)->toArray();
        $A3 = $objPHPExcel->getActiveSheet()->getCell("A3")->getValue();
        $B3 = $objPHPExcel->getActiveSheet()->getCell("B3")->getValue();
        $C3 = $objPHPExcel->getActiveSheet()->getCell("C3")->getValue();
        $D3 = $objPHPExcel->getActiveSheet()->getCell("D3")->getValue();
        $E3 = $objPHPExcel->getActiveSheet()->getCell("E3")->getValue();
        $F3 = $objPHPExcel->getActiveSheet()->getCell("F3")->getValue();
        $G3 = $objPHPExcel->getActiveSheet()->getCell("G3")->getValue();
        $H3 = $objPHPExcel->getActiveSheet()->getCell("H3")->getValue();
        $I3 = $objPHPExcel->getActiveSheet()->getCell("I3")->getValue();
        if ($A3 == "姓名" && $B3 == "性别" && $C3 == "地址" && $D3 == "生日" && $E3 == "手机号" && $F3 == "积分" && $G3 == "余额"  && $H3 == "状态" && $I3 == "时间") {
            if ($highestRow <= 5000) {
                for ($b = 3; $b <= $highestRow; $b = $b + 101) {
                    for ($i = $b + 1; $i <= $b + 100; $i++) {
                        $data['mid'] = $mid;
                        $data['name'] = $objPHPExcel->getActiveSheet()
                            ->getCell("A" . $i)
                            ->getValue();
                        $data['sex'] = $objPHPExcel->getActiveSheet()
                            ->getCell("B" . $i)
                            ->getValue();
                        $data['address'] = $objPHPExcel->getActiveSheet()
                            ->getCell("C" . $i)
                            ->getValue();
                        $data['birthday'] = $objPHPExcel->getActiveSheet()
                            ->getCell("D" . $i)
                            ->getValue();
                        $data['phone'] = $objPHPExcel->getActiveSheet()
                            ->getCell("E" . $i)
                            ->getValue();
                        $data['bonus'] = $objPHPExcel->getActiveSheet()
                            ->getCell("F" . $i)
                            ->getValue();
                        $data['balance'] = $objPHPExcel->getActiveSheet()
                            ->getCell("G" . $i)
                            ->getValue();
                        $data['state'] = $objPHPExcel->getActiveSheet()
                            ->getCell("H" . $i)
                            ->getValue();
                        $data['creattime'] = $objPHPExcel->getActiveSheet()
                            ->getCell("I" . $i)
                            ->getValue();
                        if (!empty($data['name'])) {
                            $res = M('leaduser')->data($data)->add();
                        }
                    }
                }
            } else {
                return 10001;
            }
        } else {
            return 10002;
        }

    }

}
